class UsersController < ApplicationController
  before_filter :find_project
  
  def index
    @activity_logs=Array.new
    @users = User.find(:all,:conditions=>"id!=#{current_user.id}")

    @projectids = Array.new
    MembersProject.find_all_by_user_id(current_user.id).each do |t|
      @projectids.push t.project_id
    end unless MembersProject.find_all_by_user_id(current_user.id).nil?

    @projects = Project.find(:all,:conditions=>['id IN (?)',@projectids]) if @projectids.length > 0
    
    @task_ids = Array.new
    @projects.each do |project|
      @task_ids = project.tasks.collect{|task| task.id} if project.tasks.length > 0
      activity_log =ActivityLog.find(:all, :conditions => ['task_id IN (?)',
          @task_ids], :order => 'created_at DESC', :limit => 20 )
      activity_log.each do |al|
        @activity_logs.push(al)
      end
      
    end unless @projects.nil?
    
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    
    if @user.save
      flash[:notice] = "User created successfully!"
      redirect_to :action => :index
    else
      render :action => :new
    end
    
  end
  
  def edit
    @user = User.find_by_id(params[:id])
  end

  def update
    @user = User.find_by_id(params[:id])
    
    if @user.update_attributes(params[:user])
      flash[:notice] = "User updated successfully!"
      redirect_to :action => :index
    end
    
  end

  def update_status
    user=User.find params[:id]
    user.status = params[:user][:status]
    if user.save
      redirect_to '/users' and return
    end
  end


  def show
    @user = User.find params[:id]
  end


  private
  def find_project
    @project = Project.find_by_id(params[:project_id])
  end
end
